/**
 *  Copyright Matt Silbernagel 2010
 *  
 *	This file is part of MobilePrinting.
 *
 *	MobilePrinting is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MobilePrinting is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MobilePrinting.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.uc.labs.MobilePrinting.Prefs;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Element;
import org.w3c.dom.Text;

import static edu.uc.labs.MobilePrinting.Configurator.*;
import edu.uc.labs.MobilePrinting.OSUtils.Writer;



public class PreferencesWriter extends Writer {

	
	public PreferencesWriter( Preferences p ) throws ParserConfigurationException {
		super();
		this.p = p;
	}


	public void write() throws ParserConfigurationException, IOException {
		String user_prefs = PREFS + DELIM + USER_CONF;
		File user_prefs_file = new File(user_prefs);
		logger.debug("Writing the user preference file...");
		createPrefs(user_prefs_file);
	}
	
	public void writeDefaults() throws ParserConfigurationException, IOException{
		String default_prefs = PREFS + DELIM + DEFAULT_CONF;
		logger.debug("The file to write: " + default_prefs );
		File default_prefs_file = new File(default_prefs);
		logger.debug("Writing default preferences...");
		logger.debug(p.toString());
		createPrefs(default_prefs_file);
	}
	
	private boolean createPrefs( File prefs ) throws ParserConfigurationException, IOException {
		
		boolean created = false;
		Element root = doc.createElement("Configuration");
		Element general = doc.createElement("General");
		Element advanced = doc.createElement("Advanced");
		
		Text startAtLogin = doc.createTextNode(booleanToString(p.isStartAtLogin()));
		Element e2 = doc.createElement("startatlogin");
		e2.appendChild(startAtLogin);
		
		Text deleteprinteronexit = doc.createTextNode(booleanToString(p.isDeleteOnExit()));
		Element e3 = doc.createElement("deleteprinteronexit");
		e3.appendChild(deleteprinteronexit);
		
		Text autoupdate = doc.createTextNode(booleanToString(p.isAutoUpdate()));
		Element e4 = doc.createElement("autoupdate");
		e4.appendChild(autoupdate);
		
		Text configServerText = doc.createTextNode(p.getConfigServer());
		Element configServerElement = doc.createElement("configserver");
		configServerElement.appendChild(configServerText);
		
		Text printerNameText = doc.createTextNode(p.getPrinterName());
		Element printerNameElement = doc.createElement("printername");
		printerNameElement.appendChild(printerNameText);
		
		Text logLevelText = doc.createTextNode(p.getLogLevel());
		Element logLevelElement = doc.createElement("loglevel");
		logLevelElement.appendChild(logLevelText);
		
		general.appendChild(e2);
		general.appendChild(e3);
		general.appendChild(e4);
		advanced.appendChild(configServerElement);
		advanced.appendChild(printerNameElement);
		advanced.appendChild(logLevelElement);
		
		root.appendChild(general);
		root.appendChild(advanced);
		
		doc.appendChild(root);
		
		ByteArrayOutputStream xmlBaos = new ByteArrayOutputStream();

		DOMSource source = new DOMSource(doc);
		try {
			transformer.transform(source, new StreamResult(xmlBaos));
		} catch (TransformerException e1) {
			e1.printStackTrace();
			e1.toString();
		} catch (NullPointerException e){
			e.toString();
		}
		
		FileWriter fstream = new FileWriter(prefs);
		BufferedWriter bOut = new BufferedWriter(fstream);
		bOut.write(xmlBaos.toString());
		bOut.close();
		
		return created;
	}
	
	private String booleanToString( boolean b ){
		String num = b ? "1" : "0";
		return num;
	}

	Preferences p;
}
